﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Business;
using System.Web.Security;
public partial class admin_Mng_Client : System.Web.UI.Page {
    protected void Page_Load(object sender, EventArgs e) {

    }
    /// <summary>
    /// Handles the SelectedIndexChanged event of the GridViewClient control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    /// <action>go to View_Client.aspx with client_id as query string</action>
    protected void GridViewClient_SelectedIndexChanged(object sender, EventArgs e) {
        string client_id = GridViewClient.SelectedDataKey.Value.ToString();
        try
        {
            Server.Transfer("View_Client.aspx?client_id=" + client_id);
        }catch(Exception e1){
           Response.Write(e1.ToString());
        }
    }
    /// <summary>
    /// Gets the name of the parent.
    /// </summary>
    /// <param name="parent_id">The parent_id.</param>
    /// <returns></returns>
    protected String GetParentName(string parent_id) {
        CreateConn objcon = new CreateConn();
        string query = "SELECT clientName FROM Clients" +
            " WHERE client_id = " + parent_id;
        DataTable dt = objcon.SQLConnDataTable(query);
        string parentName = dt.Rows[0]["clientName"].ToString();
        return parentName;
    }
    /// <summary>
    /// Handles the Click event of the CreateClient control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    /// <action>go to Create_Client.aspx</action>
    protected void CreateClient_Click(object sender, EventArgs e) {
        Server.Transfer("Create_Client.aspx");
    }
    /// <summary>
    /// Handles the DataBound event of the GridViewClient control.
    /// Set the text for parent name label
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void GridViewClient_DataBound(object sender, EventArgs e) {
        foreach (GridViewRow gvr in GridViewClient.Rows) {
            try {
                Label parent_idLabel = gvr.FindControl("parent_idLabel") as Label;
                Label parentNameLabel = gvr.FindControl("parentNameLabel") as Label;
                if (parent_idLabel.Text != "") {
                    parentNameLabel.Text = GetParentName(parent_idLabel.Text);
                } else {
                    parentNameLabel.Text = "None";
                }
            } catch (Exception err) {
                Console.Write("Err msg:" + err.Message);
            }
        }
    }
    protected void LoginStatus1_LoggingOut(object sender, LoginCancelEventArgs e)
    {


        Session.Contents.Remove("UserDetails");
        Session.Abandon();
        Session.Contents.RemoveAll();
        FormsAuthentication.SignOut();
        Response.Redirect("../Default.aspx");
    }

}
